// Copyright 2012 dmitry.a.novikov@gmail.com
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// CnsZ::DObject declaration


#ifndef _INCLUDED_CNZ_CHARINDEX_H_
#define _INCLUDED_CNZ_CHARINDEX_H_

#include "valuearray.h"

namespace CnsZ {

  class CharIndex {
  protected:
    friend class CharCompare;
    class Node {
      Node() {}
    public:
      P<Object> item;
      size_t numchars;
      wchar_t key[1];

      void add(Object* item, const wchar_t* key, size_t numchars);
      static Node* New(Object* item, const wchar_t* key, size_t numchars);
    };

    class T : public BaseArrayTraits<Node*, T> {
    public:
      static void destruct(iterator elem) {
        delete *elem;
      }
      static void destruct(iterator beg, iterator end) {
        while(beg<end) { destruct(beg++); }
      }
    };

    class NodePtrArray : public A<Node*, T> {
    public:
      iterator lookup(const wchar_t* key);
      const_iterator lookup(const wchar_t* key) const;
    };

    Node* find(const wchar_t* key);

    NodePtrArray root;
  public:
    void add(Object* item, const wchar_t* key, size_t keychars = -1);
    P<Object> get(const wchar_t* key) const;

  };

}

#endif //#ifndef _INCLUDED_CNZ_CHARINDEX_H_